package com.controller.backend;

import com.biz.MenuInfoBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.ManagerInfo;
import com.entity.model.MenuInfo;
import com.util.StringFliterUtil;
import com.util.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by asan on 2017/09/17.
 */
@Controller
@RequestMapping("/backend/security/menu")
public class MenuInfoController extends BaseController<MenuInfo> {
    @Autowired
    private MenuInfoBiz menuInfoBiz;

    //后台菜单列表
    @RequestMapping(value = "index")
    public String shopMenu() throws Exception {
        return "security/menuInfo/list";
    }

    //后台菜单详情页面
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "security/menuInfo/form";
    }

    //删除后台菜单页面(连同子菜单一起删除)
    @RequestMapping(value = "delMenuForm")
    public String delMenuForm() throws Exception {
        return "security/menuInfo/delMenuForm";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "getMenu")
    @ResponseBody
    public Result getMenu(HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (managerInfo == null) {
            return ResultGenerator.genFailResult("/backend/loginIndex");
        } else {
            List<MenuInfo> list;
            if (1 == managerInfo.getId())//超级管理员
            {
                list = menuInfoBiz.findAll();
                session.setAttribute(SystemConstant.SESSION_RIGHT_LIST, list);
            } else {
                list = menuInfoBiz.findResAuthorized(managerInfo.getId(), 1);
                session.setAttribute(SystemConstant.SESSION_RIGHT_LIST, menuInfoBiz.findResAuthorized(managerInfo.getId(), null));
            }
            Map res = new HashMap(2);
            res.put("account", managerInfo);
            res.put("menu", list);
            return ResultGenerator.genSuccessResult(res);
        }
    }

    //获取所有后台菜单按钮
    @RequestMapping(value = "getMenuInfo")
    @ResponseBody
    public Result getMenuInfo(HttpSession session) {
        List<MenuInfo> list = menuInfoBiz.findAll();
        session.setAttribute(SystemConstant.SESSION_RIGHT_LIST, list);
        return ResultGenerator.genSuccessResult(list);

    }

    //新增门店菜单
    @RequestMapping(value = "addShopMenu", method = RequestMethod.POST)
    @ResponseBody
    public Result addShopMenu(MenuInfo menuInfo) {
        if (menuInfo != null) {
            HttpSession session = getRequest().getSession();
            ManagerInfo adminView = null;
            adminView = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);

            adminView.setUserName(StringFliterUtil.cleanXSS(adminView.getUserName()));

            menuInfo.setCreateUserId(adminView.getId());
            menuInfo.setCreateUserName(adminView.getUserName());
            menuInfoBiz.save(menuInfo);
            return ResultGenerator.genSuccessResult(menuInfo);
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }

    //删除门店菜单(连同子菜单一起删除)
    @RequestMapping(value = "delMenu")
    @ResponseBody
    public Result delMenu(@RequestParam Integer id) {
        if (id != null) {
            Condition cond = new Condition(MenuInfo.class);
            cond.createCriteria().andEqualTo("parentId", id);

            baseBiz.deleteById(id);
            baseBiz.deleteByCond(cond);
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }


    @RequestMapping(value = "getList")
    @ResponseBody
    public Result getList(MenuInfo menuInfo) {
        List<MenuInfo> list = baseBiz.findAll();
        return ResultGenerator.genSuccessResult(list);
    }

}
